package ApiController

import (
	"bytes"
	"el/app/libs"
	"el/bootstrap/driver"
	"encoding/json"

	"github.com/labstack/echo/v4"
)

type ElasticsearchController struct {
}

func (my *ElasticsearchController) Create(c echo.Context) error {

	doc := map[string]interface{}{
		"title":   "你看到外面的世界是什么样的？",
		"content": "外面的世界真的很精彩",
	}
	var buf bytes.Buffer
	if err := json.NewEncoder(&buf).Encode(doc); err != nil {
		return libs.Error(c, 1, err.Error())
	}
	res, err := driver.Elasticsearch.Index("test", &buf, driver.Elasticsearch.Index.WithDocumentType("doc"))

	if err != nil {
		return libs.Error(c, 1, err.Error())
	}
	defer res.Body.Close()

	return libs.Ok(c, res.String())
}
